<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="用户昵称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="用户邮箱"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="用户性别（0男 1女 2未知）" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="用户性别（0男 1女 2未知）"></el-input>
    </el-form-item>
    <el-form-item label="头像路径" prop="avatar">
      <el-input v-model="dataForm.avatar" placeholder="头像路径"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="盐加密" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐加密"></el-input>
    </el-form-item>
    <el-form-item label="帐号状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="帐号状态（0正常 1停用）"></el-input>
    </el-form-item>
    <el-form-item label="删除标志（0代表存在 1代表删除）" prop="deleteStatus">
      <el-input v-model="dataForm.deleteStatus" placeholder="删除标志（0代表存在 1代表删除）"></el-input>
    </el-form-item>
    <el-form-item label="最后登陆时间" prop="loginDate">
      <el-input v-model="dataForm.loginDate" placeholder="最后登陆时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          updateTime: '',
          userName: '',
          email: '',
          phone: '',
          sex: '',
          avatar: '',
          password: '',
          salt: '',
          status: '',
          deleteStatus: '',
          loginDate: '',
          createTime: ''
        },
        dataRule: {
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '用户邮箱不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '用户性别（0男 1女 2未知）不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像路径不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          salt: [
            { required: true, message: '盐加密不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '帐号状态（0正常 1停用）不能为空', trigger: 'blur' }
          ],
          deleteStatus: [
            { required: true, message: '删除标志（0代表存在 1代表删除）不能为空', trigger: 'blur' }
          ],
          loginDate: [
            { required: true, message: '最后登陆时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/member/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.updateTime = data.user.updateTime
                this.dataForm.userName = data.user.userName
                this.dataForm.email = data.user.email
                this.dataForm.phone = data.user.phone
                this.dataForm.sex = data.user.sex
                this.dataForm.avatar = data.user.avatar
                this.dataForm.password = data.user.password
                this.dataForm.salt = data.user.salt
                this.dataForm.status = data.user.status
                this.dataForm.deleteStatus = data.user.deleteStatus
                this.dataForm.loginDate = data.user.loginDate
                this.dataForm.createTime = data.user.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/member/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'updateTime': this.dataForm.updateTime,
                'userName': this.dataForm.userName,
                'email': this.dataForm.email,
                'phone': this.dataForm.phone,
                'sex': this.dataForm.sex,
                'avatar': this.dataForm.avatar,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'status': this.dataForm.status,
                'deleteStatus': this.dataForm.deleteStatus,
                'loginDate': this.dataForm.loginDate,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
